package com.intellect.syncproject;

import java.io.File;
import java.io.IOException;

public class SynchWorkItem {

	enum ActionType {
		copy,
		create,
		delete,
		replace
	}
	
	ActionType action;
	boolean toTarget;
	
	File actionable;
	File toReplace;
	
	public SynchWorkItem() {}
	
	public SynchWorkItem(File actionable, ActionType action, boolean toTarget) {
		this.action = action;
		this.toTarget = toTarget;
		this.actionable = actionable;
	}
	
	public SynchWorkItem(File actionable, File toReplace, ActionType action) {
		this.action = action;
		this.actionable = actionable;
		this.toReplace = toReplace;
	}
	
	public static SynchWorkItem fromString(String savedString) {
		String[] parts = savedString.split("~");
		SynchWorkItem workItem = new SynchWorkItem();
		workItem.action = ActionType.valueOf(parts[0]);
		workItem.toTarget = (parts[1].equals(">")) ? true : false;
		workItem.actionable = new File(parts[2]);
		
		return workItem;
	}

	public ActionType getAction() {
		return action;
	}

	public void setAction(ActionType action) {
		this.action = action;
	}

	public boolean isToTarget() {
		return toTarget;
	}

	public void setToTarget(boolean toTarget) {
		this.toTarget = toTarget;
	}

	public File getActionable() {
		return actionable;
	}

	public void setActionable(File actionable) {
		this.actionable = actionable;
	}
	
	public String toSaveString() throws IOException {

		String fileName = actionable.getCanonicalPath();
		String direction = (toTarget) ? ">" : "<";
		String fileOrDir = (actionable.isDirectory()) ? "Directory" : "File";
		
		return String.format("%s~%s~%s~%s ", action.toString(), direction, fileName, fileOrDir );
	}
}
